package com.highspeed.bdk.gateway.socket.analyze.base;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.Map;

public abstract class AbstractResolver implements Resolver {
    private Map container = new HashMap(128);
    protected Log logger = LogFactory.getLog(getClass());

    @Override
    public Map resolve(byte[] response) {
        clear();
        getContainer().put(OFF_SET, START);
        doResolveHeader(response);
        doResolveBody(response);
        doResolveTail(response);
        return container;
    }

    @Override
    public void clear() {
        getContainer().clear();
    }

    @Override
    public int getOffset() {
        return (int) container.get(OFF_SET);
    }

    @Override
    public void offset1() {
        container.put(OFF_SET, getOffset() + BYTE_LENGTH);
    }

    @Override
    public void offset2() {
        container.put(OFF_SET, getOffset() + SHORT_LENGTH);
    }

    @Override
    public void offset4() {
        container.put(OFF_SET, getOffset() + INT_LENGTH);
    }

    @Override
    public void offset8() {
        container.put(OFF_SET, getOffset() + LONG_LENGTH);
    }

    @Override
    public void offsetN(int n) {
        container.put(OFF_SET, getOffset() + n);
    }

    @Override
    public Object get(String key) {
        return container.get(key);
    }

    @Override
    public void put(String key, Object value) {
        container.put(key, value);
    }

    protected abstract Map doResolveHeader(byte[] response);

    protected abstract Map doResolveBody(byte[] response);

    protected abstract Map doResolveTail(byte[] response);

    @Override
    public Map getContainer() {
        return container;
    }

    @Override
    public void setContainer(Map container) {
        this.container = container;
    }


}
